package com.cnswhy.cloud.server.comsumer.consumer.split;

import com.cnswhy.cloud.common.core.entity.constant.CnswhyConstant;
import com.cnswhy.cloud.common.redis.annotation.RedisLock;
import com.cnswhy.cloud.common.redis.model.LockTimeoutStrategy;
import com.cnswhy.cloud.common.redis.service.IdGeneratorService;
import com.cnswhy.cloud.common.redis.service.RedisService;
import com.cnswhy.cloud.server.common.entity.enumeration.TransType;
import com.cnswhy.cloud.server.common.entity.enumeration.WalletType;
import com.cnswhy.cloud.server.common.entity.mq.RegisterMsg;
import com.cnswhy.cloud.server.comsumer.event.RegisterMessageEvent;
import com.cnswhy.cloud.server.comsumer.event.VerifyMessageEvent;
import com.cnswhy.cloud.server.core.entity.CpsTransOrder;
import com.cnswhy.cloud.server.core.entity.Customer;
import com.cnswhy.cloud.server.core.entity.CustomerWallet;
import com.cnswhy.cloud.server.core.oem.SystemService;
import com.cnswhy.cloud.server.core.service.CustomerSplitService;
import com.cnswhy.cloud.server.core.service.customer.CustomerService;
import com.cnswhy.cloud.server.core.wallet.WalletService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 给直推上级用户奖励
 */
@Component
@Slf4j
public class RegisterRewordConsumer implements VerifyMessageEvent {
    @Autowired
    CustomerService customerService;
    @Autowired
    WalletService walletService;
    @Autowired
    SystemService systemService;
    @Autowired
    IdGeneratorService idGeneratorService;
    @Autowired
    RedisService redisService;
    @Override
    public void verify(RegisterMsg registerMsg) {
        Long registerReword = systemService.getRegisterReword();
        log.info("给直推上级用户奖励:{} {} {}",registerMsg.getCustomerId(),registerMsg.getLoginName(),registerReword);
        if(registerReword <= 0L){
            return;
        }
        if(rewordFlag(registerMsg.getCustomerId())){
            return;
        }
        Customer customer = customerService.getById(registerMsg.getCustomerId());
        if(customer.getPromoterId() != null){
            Customer promoter = customerService.getById(customer.getPromoterId());
            if(promoter != null){
                CustomerWallet customerWallet = walletService.checkAndCreate(promoter.getId(), WalletType.REWORD,0L);
                walletService.add(customerWallet.getCustomerId(), WalletType.REWORD, TransType.REGISTER_REWORD,registerReword,idGeneratorService.genCommonIdString());
            }
        }
        log.info("给直推上级用户奖励：完成");

    }
    @RedisLock(name = "rewordFlag", waitTime = 10,leaseTime = 60,keys = {"#customerId"},lockTimeoutStrategy = LockTimeoutStrategy.FAIL_FAST)
    private boolean rewordFlag(Long customerId){
        String abc = (String)redisService.hget("cps.createQrcode" , customerId.toString());
        if (StringUtils.isNotBlank(abc)) {
            return true;
        }
        redisService.hset("cps.createQrcode" , customerId.toString(), customerId.toString(), CnswhyConstant.DAY_SECONDS / 24);
        return false;
    }
}
